#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>
#define ERROR_CHECK(ret, num, msg) \
    {                              \
        if (ret == num)            \
        {                          \
            perror(msg);           \
            exit(1);               \
        }                          \
    }
int main(int argc, char *argv[])
{
    printf("hello\n");
    __pid_t pid;
    pid = fork();
    ERROR_CHECK(-1, pid, "fork");
    if (pid == 0)
    {

        while (1)
        {
            
        printf("pid%d", pid);
        printf("我是子进程 pid = %d, ppid = %d\n", getpid(), getppid());
        sleep(2);
        }
        exit(10);
    }
    else if (pid > 0)
    {

        printf("pid%d", pid);
        printf("我是父进程 pid = %d, ppid = %d\n", getpid(), getppid());
        int wait_stat;
        wait(&wait_stat);
        printf("%d",wait_stat);
        if (WIFEXITED(wait_stat))
        {
            printf("子进程正常退出\t");
            printf("%d\n",WEXITSTATUS(wait_stat));
        }else if(WIFSIGNALED(wait_stat)){
            printf("信号导致退出 %d\n", WTERMSIG(wait_stat));
        }
        return 0;
    }
}
